package com.hxm.demo.java.io;

import java.io.*;

/**
 * @author huangxiaoming
 * @date 2020/8/13
 **/
public class CopyFile {

    public static void main(String[] args) throws IOException {

        //字节流
//        File file1 = new File("src/main/resources/static/test.txt");
//        FileInputStream inputStream = new FileInputStream(file1);
//        FileOutputStream outputStream = new FileOutputStream("src/main/resources/static/test1.txt");
//        byte[] bytes = new byte[1024];
//        while (inputStream.read(bytes) !=-1){
//                outputStream.write(bytes);
//        }

        //字符流
        FileInputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        FileOutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            File file = new File("src/main/resources/static/test.txt");
            inputStream = new FileInputStream(file);
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);

            outputStream = new FileOutputStream("src/main/resources/static/test1.txt");
            outputStreamWriter = new OutputStreamWriter(outputStream);
            bufferedWriter = new BufferedWriter(outputStreamWriter);

            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                bufferedWriter.write(line + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bufferedWriter.flush();
            inputStream.close();
            inputStreamReader.close();
            bufferedReader.close();
            outputStream.close();
            outputStreamWriter.close();
            bufferedWriter.close();
        }

    }
}
